<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="java.io.File"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="java.util.Date"%>
<%@page import="com.pb.news.entity.News"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<%@include file="../common/common.jsp" %>
<body>
<%
	//接收增加的新闻信息，并调用后台方法，将新闻信息插入数据库
	request.setCharacterEncoding("utf-8");
	// 解析请求之前，判断是否为文件上传类型
	boolean isMutiPart = ServletFileUpload.isMultipartContent(request);
	// 设置文件上传路径
	// getSession获得的是HttpSession
	String fileUploadPath = request.getSession().getServletContext().getRealPath("upload/");
	News news = new News();
	news.setCreateDate(new Date());
	// 判断目标路径文件是否存在，不存在自动创建
	File saveDir = new File(fileUploadPath);
	if(!saveDir.exists()){
		saveDir.mkdir();
	} 
	if(isMutiPart){ // 处理文件上传
		// 创建文件上传的核心类ServletFileUpload
		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		// 解析请求
		List<FileItem> items = upload.parseRequest(request);
		// 获取迭代器
		Iterator<FileItem> iters = items.iterator();
		while(iters.hasNext()){
			FileItem item = iters.next();
			// 判断fileItem是文本域还是文件域
			if(item.isFormField()){ //是普通文本表单控件
				String fieldName = item.getFieldName(); // 获取文本域字段name的值
				// 获取文本域value的值
				if(fieldName.equals("categoryId")){
					news.setCategoryId(Integer.parseInt(item.getString()));
				}else if(fieldName.equals("title")){
					news.setTitle(item.getString("utf-8"));
				}else if(fieldName.equals("author")){
					news.setAuthor(item.getString("utf-8"));
				}else if(fieldName.equals("summary")){
					news.setSummary(item.getString("utf-8"));
				}else if(fieldName.equals("newscontent")){
					news.setContent(item.getString("utf-8"));
				}
			}else{ // 是文件表单控件
				String fileName = item.getName(); //上传文件名称（全路径）
				if(fileName != null && !fileName.equals("")){
					File fullFile = new File(fileName);
					File saveFile = new File(fileUploadPath,fullFile.getName());
					item.write(saveFile); //上传操作
					news.setPicPath(fullFile.getName());
				}
			}
		}
		//调用后台方法，将新闻信息插入数据库
		boolean flag=newsService.addNews(news);
		if(flag){ //成功
			// 重定向
			response.sendRedirect("newsDetailList.jsp");
		}else{ // 不成功
			// 转发
			//request.getRequestDispatcher("newsDetailList.jsp").forward(request, response);
		%>
			<jsp:forward page="newsDetailCreate.jsp"/>
		<%}
	
	}
	 

	/* int categoryId=Integer.parseInt(request.getParameter("categoryId"));  //新闻类别
	String title=request.getParameter("title");   //新闻标题
	String author=request.getParameter("author"); //新闻作者
	String summary=request.getParameter("summary"); //新闻摘要
	String content=request.getParameter("newscontent"); //新闻内容 */
	
	/* //将新闻信息封装为一个新闻对象
	News news =new News();
	news.setCategoryId(categoryId);
	news.setTitle(title);
	news.setAuthor(author);
	news.setSummary(summary);
	news.setContent(content);
	news.setCreateDate(new Date()); */
	%>
	
</body>
</html>